import { LabeledValue } from 'ant-design-vue/lib/select';
import { componentMap } from '@/components/Form/src/componentMap';
import { BasicColumn } from '@/components/Table';
import { FormSchema } from '@/components/Table';
const components = componentMap.keys();
const componentsOptions: LabeledValue[] = [];
for (const item of components) {
  componentsOptions.push({
    label: item,
    value: item,
  });
}
export const columns: BasicColumn[] = [
  {
    title: '字段名',
    align: 'center',
    dataIndex: 'name',
  },
  {
    title: '备注',
    align: 'center',
    dataIndex: 'remark',
  },
  {
    title: '字段长度',
    align: 'center',
    dataIndex: 'fieldLength',
  },
  {
    title: '是否主键',
    align: 'center',
    dataIndex: 'isPrimaryKey',
  },
  {
    title: '是否显示',
    align: 'center',
    dataIndex: 'isShow',
  },
  {
    title: '是否编辑',
    align: 'center',
    dataIndex: 'isEdit',
  },
  {
    title: '',
    align: 'center',
    dataIndex: 'dbType',
  },
  {
    title: '组件类型',
    align: 'center',
    dataIndex: 'component',
  },
];

export const searchFormSchema: FormSchema[] = [];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'tableId',
    label: '对应表id',
    component: 'Input',
    show: false,
  },
  {
    field: 'name',
    label: '字段名',
    component: 'Input',
    required: true,
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    required: true,
  },
  {
    field: 'dbType',
    label: '数据类型',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        dictCode: 'gen_data_type',
        onChange(val: string, allData: Recordable) {
          // !!! 重点
          if (!allData) return;
          const { data } = allData;
          data.ext1 === 'Y'
            ? (formModel.fieldLength = data.ext2)
            : (formModel.fieldLength = void 0);
        },
      };
    },
  },
  {
    field: 'fieldLength',
    label: '字段长度',
    component: 'InputNumber',
    ifShow: ({ values }) =>
      values.dbType &&
      values.dbType.indexOf('date') === -1 &&
      values.dbType.indexOf('text') === -1,
    required: true,
    componentProps: {
      min: 1,
    },
  },
  {
    field: 'component',
    label: '组件类型',
    component: 'Select',
    required: true,
    componentProps: {
      options: componentsOptions,
    },
  },
  {
    field: 'isPrimaryKey',
    label: '是否主键',
    component: 'ApiRadioGroup',
    required: true,
    defaultValue: 'Y',
    componentProps: {
      dictCode: 'yes_or_no',
    },
  },
  {
    field: 'isShow',
    label: '是否显示',
    component: 'ApiRadioGroup',
    required: true,
    defaultValue: 'Y',
    componentProps: {
      dictCode: 'yes_or_no',
    },
  },
  {
    field: 'isEdit',
    label: '是否编辑',
    required: true,
    defaultValue: 'Y',
    component: 'ApiRadioGroup',
    componentProps: {
      dictCode: 'yes_or_no',
    },
  },
];
